package cn.iocoder.yudao.module.cms.agreement.service;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.cms.agreement.bo.AgreementIdentifyBO;
import cn.iocoder.yudao.module.cms.agreement.dal.dataobject.AgreementDO;
import cn.iocoder.yudao.module.cms.agreement.dal.mysql.AgreementMapper;
import cn.iocoder.yudao.module.cms.agreement.vo.AgreementCreateReqVO;
import cn.iocoder.yudao.module.cms.agreement.vo.AgreementPageReqVO;
import cn.iocoder.yudao.module.cms.agreement.vo.AgreementUpdateReqVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.api.cms.ErrorCodeConstants.AGREEMENT_DUPLICATE;
import static cn.iocoder.yudao.module.api.cms.ErrorCodeConstants.AGREEMENT_NOT_EXISTS;

/**
 * 协议规则 Service 实现类
 *
 * @author 山野羡民
 */
@Service
@Validated
public class AgreementServiceImpl implements AgreementService {

    @Resource
    private AgreementMapper agreementMapper;

    @Override
    public Long createAgreement(AgreementCreateReqVO createReqVO) {
        validateAgreementDuplicate(createReqVO.getIdentify());
        // 插入
        AgreementDO agreementDO = BeanUtils.toBean(createReqVO, AgreementDO.class);
        agreementMapper.insert(agreementDO);
        // 返回
        return agreementDO.getId();
    }

    @Override
    public void updateAgreement(AgreementUpdateReqVO updateReqVO) {
        // 校验存在
        validateAgreementExists(updateReqVO.getId());
        // 更新
        AgreementDO agreementDO = BeanUtils.toBean(updateReqVO, AgreementDO.class);
        agreementMapper.updateById(agreementDO);
    }

    @Override
    public void deleteAgreement(Long id) {
        // 校验存在
        AgreementDO agreementDO = validateAgreementExists(id);
        // 删除
        agreementMapper.deleteById(agreementDO);
    }

    private AgreementDO validateAgreementExists(Long id) {
        AgreementDO agreementDO = agreementMapper.selectById(id);
        if (agreementDO == null) {
            throw exception(AGREEMENT_NOT_EXISTS);
        }
        return agreementDO;
    }

    private void validateAgreementDuplicate(String identify) {
        AgreementDO agreementDO = agreementMapper.selectOne(AgreementDO::getIdentify, identify);
        if (agreementDO != null) {
            throw exception(AGREEMENT_DUPLICATE);
        }
    }

    @Override
    public AgreementDO getAgreement(Long id) {
        return agreementMapper.selectById(id);
    }

    @Override
    public PageResult<AgreementDO> getAgreementPage(AgreementPageReqVO pageReqVO) {
        return agreementMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AgreementIdentifyBO> getAllIdentify() {
        return agreementMapper.selectAllIdentify();
    }

    @Override
    public AgreementDO getContent(String identify) {
        return agreementMapper.selectOne(AgreementDO::getIdentify, identify);
    }

}
